<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    pieData: {  // 确保有这个props定义
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    pieData: {  // 监听数据变化
      deep: true,
      handler(val) {
        console.log('PieChart data changed:', val)
        this.initChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      // 添加日志，检查组件是否接收到数据
      console.log('PieChart mounted, data:', this.pieData)
      this.initChart()
    })
  },
  methods: {
    initChart() {
      // 添加日志，检查initChart被调用时的数据
      console.log('PieChart initChart called with data:', this.pieData)
      
      this.chart = echarts.init(this.$el)
      
      // 默认数据，防止pieData为空时图表无法显示
      const defaultData = [
        { value: 435, name: '珍珠奶茶' },
        { value: 310, name: '芝士奶盖' },
        { value: 234, name: '水果茶' },
        { value: 195, name: '满杯水果' },
        { value: 348, name: '乌龙奶茶' }
      ]
      
      // 使用实际数据或默认数据
      const pieData = (this.pieData && this.pieData.length > 0) ? this.pieData : defaultData
      const legendData = pieData.map(item => item.name)
      
      this.chart.setOption({
        backgroundColor: '#ffffff',
        title: {
          text: '奶茶销售占比',
          textStyle: {
            fontSize: 16,
            fontWeight: 'normal',
            color: '#303133'
          },
          left: 'center',
          top: 0
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c}杯 ({d}%)',
          backgroundColor: 'rgba(255, 255, 255, 0.9)',
          borderColor: '#f5f5f5',
          borderWidth: 1,
          textStyle: {
            color: '#666'
          },
          padding: [12, 16]
        },
        legend: {
          left: 'center',
          bottom: '0',
          data: legendData,
          textStyle: {
            color: '#666',
            fontSize: 12
          },
          icon: 'circle',
          itemWidth: 10,
          itemHeight: 10,
          itemGap: 20
        },
        series: [
          {
            name: '奶茶销量',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '45%'],
            avoidLabelOverlap: false,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.2)'
              }
            },
            label: {
              normal: {
                show: false
              },
              emphasis: {
                show: true,
                textStyle: {
                  fontSize: '14',
                  fontWeight: 'bold'
                }
              }
            },
            labelLine: {
              normal: {
                show: false
              }
            },
            data: pieData,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            color: ['#409EFF', '#36A3F7', '#F4516C', '#34BFA3', '#FFB980']
          }
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.chart {
  position: relative;
  width: 100%;
  height: 100%;
}
</style>